package controllers.bluehat;

import java.util.ArrayList;
import java.util.List;

import models.Focus;
import models.Profile;
import models.User;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import play.mvc.With;
import utils.exceptions.ValidationException;
import utils.json.data.ErrorJSON;
import utils.json.data.FocusJSON;
import utils.table.DataTable;
import utils.table.DataTable.DataTablesParameters;
import controllers.Check;
import controllers.Secure;
import controllers.Security;
import controllers.user.UserController;

@With(Secure.class)
@Check(Security.BLUEHAT_ROLE)
public class FocusController extends UserController {

	public static void save(String body) throws ValidationException{
		User user = getCurrentUser();
		
		JsonObject json = new JsonParser().parse(body).getAsJsonObject();
        Long id = json.get("id") != null ? json.get("id").getAsLong() : null;
        String title = json.get("title") != null ? json.get("title").getAsString() : null;
        String description = json.get("description") != null ? json.get("description").getAsString() : null;

        Focus focus = null;
        if(id != null) focus = Focus.getById(id);
        else focus = new Focus();
                        
        focus.title = title;
        focus.description = description;
        
        validation.valid(focus);
        if(validation.hasErrors()) {
	        ErrorJSON errors = new ErrorJSON(validation.errors());
	        //JsonObject test = new JsonParser().parse(errors.toJson()).getAsJsonObject();
	        //System.out.println(test.toString());
	        //error(errors.toJson());
	        throw new ValidationException("Validation failed!!", errors);
        } else {
        	// Owner
        	user.addFocus(focus);
        	// Participants!
            JsonArray participants = json.getAsJsonArray("participants");
            for(int i = 0; i < participants.size(); i++) {
            	JsonObject participantJSON = participants.get(i).getAsJsonObject();
            	Long participantId = participantJSON.get("id").getAsLong();
            	User participant = User.getById(participantId);
            	focus.addParticipant(participant);
            }
            // Save!
            focus.save();
            renderJSON(new FocusJSON(focus)); // TODO render using a template
        }               
	}
	
	public static void delete(Long id) {
		Focus focus = Focus.getById(id);
		focus.delete();
	}
	
	public static void detail(Long id) {
		Focus focus = Focus.getById(id);
		renderJSON(new FocusJSON(focus));
	}
	
	public static void list() {
		final DataTablesParameters dtp = new DataTable.DataTablesParameters(request);
        User user = getCurrentUser();
        
        DataTable<Focus> table = new DataTable<Focus>(user.ownedFocus(), dtp);
        
        List<FocusJSON> jsonList = new ArrayList<FocusJSON>();
        for(Focus f : table.getRows()) {
                jsonList.add(new FocusJSON(f));
        }
        table.setRows(jsonList);
        renderJSON(table);
	}
}
